package cmarter.dev.chpt2.comparators;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

import cmarter.dev.chpt2.Utils;
import cmarter.dev.entities.Apple;

public class Java8Comparators {
	
	private static List<Apple> apples;

	public static void main(String[] args) {
		init();

		traditionalSortComparatorExample();
		lambdaSortComparatorExample();
		
	}
	
	private static void init() {
		apples = Arrays.asList(new Apple("green", 90d, "Granny Smith"),
				   new Apple("green", 150d, "Granny Smith"),
				   new Apple("green", 190d, "smith"),
				   new Apple("green", 149d, "smith"),
				   new Apple("green", 160d, "smith"),
				   new Apple("red", 190d, "Fuji"));
	}
	
	private static void traditionalSortComparatorExample() {
		List<Apple> sortedApples = new ArrayList<>(apples);
		
		sortedApples.sort(new Comparator<Apple>() {
			public int compare(Apple a1, Apple a2) {
				return a1.getWeight().compareTo(a2.getWeight());
				}
			});
		System.out.println("unsorted: ");
		Utils.printList(apples);
		System.out.println("\nsorted: ");
		Utils.printList(sortedApples);
	}
	
	private static void lambdaSortComparatorExample() {
		List<Apple> sortedApples = new ArrayList<>(apples);	
		
		sortedApples.sort((Apple a1, Apple a2) -> a1.getWeight().compareTo(a2.getWeight()));
		
		System.out.println("unsorted: ");
		Utils.printList(apples);
		System.out.println("\nsorted: ");
		Utils.printList(sortedApples);
	}

}
